﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using HOPU.OnlineSchool.Application.Teacher.Dtos;
using HOPU.OnlineSchool.Core;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HOPU.OnlineSchool.Application.Teacher.Service
{
    public class TeacherService : ITeacherService,ITransient
    {
        /// <summary>
        /// 仓储查询
        /// </summary>
        private readonly IRepository<EduTeacher> _teacherRepository;

        public TeacherService(IRepository<EduTeacher> teacherRepository)
        {
            _teacherRepository = teacherRepository;
        }
        

        public async Task<PagedList<TeacherListOutput>> GetTeacherAsnyc(int page, int limit, TeacherListInput input)
        {
            var query = _teacherRepository.Where(a => a.IsDeleted == 0);
            if (!string.IsNullOrWhiteSpace(input.Name))
            {
                query = query.Where(a => a.Name.Contains(input.Name));
            }
            if (input.Level > 0)
            {
                query = query.Where(a => a.Level == input.Level);
            }
            if (input.Bagin.HasValue)
            {
                //当前时间在它之后
                query = query.Where(a => a.GmtCreate >= input.Bagin.Value);
            }
            if (input.End.HasValue)
            {
                //当前时间在它之前
                query = query.Where(a => a.GmtCreate <= input.End.Value);
            }

            var list = await query.OrderByDescending(a => a.GmtCreate).ToPagedListAsync(page,limit);
            return list.Adapt<PagedList<TeacherListOutput>>();
        }
    }
}
